var ctxPath = "/education";

$.getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
};
/**
 * Javascript API调用ActionSheet
 */
!function ($) {
    var $myAs = $('#J_ActionSheet');

    $('#J_ShowActionSheet').on('click', function () {
        $myAs.actionSheet('open');
    });

    $('#J_Cancel').on('click', function () {
        $myAs.actionSheet('close');
    });

    var detailId = $.getUrlParam('id');

    /**
     * 初始化游学产品详情方法
     * @param id
     */
    var initIntegralDetail = function(id) {
        $.ajax({
            url: ctxPath + '/wx/api/pointReward/getProductById',
            type: 'post',
            dataType: 'json',
            data: {pointRewardProductId: id},
            success: function (result) {
                if(result.returnCode == 0) {
                    var data = result.pointRewardProductPo;

                    if (data != null) {
                        $("input[name='id']").val(data.id);
                        $("input[name='code']").val(data.code);

                        $("#showImage").attr("src", data.showImage);
                        $("#buyShowImage").attr("src", data.showImage);

                        $("#name").html(data.name);
                        $("#buyTitle").html(data.name);

                        $("#price").html("¥" + data.price);
                        $("#buyPrice").html("¥" + data.price);

                        $("#detail").html(data.detail);
                    }
                } else {
                    alert(result.returnMsg);
                }
            }
        });
    };

    initIntegralDetail(detailId);

    $('#J_Quantity').spinner({
        input: '.J_Input',
        add: '.J_Add',
        minus: '.J_Del',
        unit: function () {
            return 1;
        },
        max: function () {
            return 100;
        },
        callback: function (value, $ele) {
            var buyPrice = ($("#price").html().replace("¥", ""));
            $("#buyPrice").html("¥" + (buyPrice * value));
        }
    });

    $("#createOrderBtn").click(function () {
        var request = {};

        request.prProductId = $("input[name='id']").val();
        request.customerName = $("input[name='customerName']").val();
        request.phone = $("input[name='phone']").val();
        request.address = $("input[name='address']").val();
        request.amount = $("input[name='J_Input']").val();

        if(null == request.customerName || "" == request.customerName){
            alert("请输入说活人姓名！");
            return;
        }

        if(null == request.phone || "" == request.phone || !(/^1[3|4|5|6|7|8|9]\d{5,9}$/.test(request.phone))){
            alert("请输入正确的手机号！");
            return;
        }

        if(null == request.address || "" == request.address){
            alert("请输入收货地址！");
            return;
        }

        //创建订单，跳转至支付页面
        $.ajax({
            url: ctxPath + '/wx/api/pointReward/createOrder',
            contentType: "application/json",
            type: 'post',
            data: JSON.stringify(request),
            success: function (result) {
                if(result.returnCode == 0) {
                    // alert("购买成功，发货后请在积分商城订单中查看快递单号！")
                    window.location.href = "/education/wx/integralProductPay.html?orderCode=" + result.orderCode;
                } else {
                    alert(result.returnMsg);
                    if(result.returnMsg=="客户未登录！"){
                        window.location.href ="/education/wx/customerLogin.html?redirect=/education/wx/integralDetail.html?id=" + detailId;
                    }
                }
            }
        });
    })
}(jQuery);